home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / hplip / ui4 / plugindialog.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  10.0 KB  |  294 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2001-2009 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Authors: Don Welch
  20. #
  21.  
  22.  
  23. # Local
  24. from base.g import *
  25. from base import device, utils
  26. from prnt import cups
  27. from base.codes import *
  28. from ui_utils import *
  29. from installer.core_install import CoreInstall
  30.  
  31. # Qt
  32. from PyQt4.QtCore import *
  33. from PyQt4.QtGui import *
  34.  
  35. # Ui
  36. from plugindialog_base import Ui_Dialog
  37.  
  38.  
  39. PAGE_SOURCE = 0
  40. # PAGE_LICENSE = 1 # part of plug-in itself, this is a placeholder
  41. PAGE_MAX = 1
  42.  
  43.  
  44.  
  45. class PluginDialog(QDialog, Ui_Dialog):
  46.     def __init__(self, parent, install_mode=PLUGIN_NONE):
  47.         QDialog.__init__(self, parent)
  48.         self.install_mode = install_mode
  49.         self.plugin_path = None
  50.         self.result = False
  51.         self.core = CoreInstall()
  52.         self.core.set_plugin_version()
  53.         self.setupUi(self)
  54.         self.initUi()
  55.  
  56.         QTimer.singleShot(0, self.showSourcePage)
  57.  
  58.  
  59.     def isPluginInstalled(self):
  60.         return self.core.check_for_plugin()
  61.  
  62.  
  63.     def initUi(self):
  64.         # connect signals/slots
  65.         self.connect(self.CancelButton, SIGNAL("clicked()"), self.CancelButton_clicked)
  66.         self.connect(self.NextButton, SIGNAL("clicked()"), self.NextButton_clicked)
  67.  
  68.         # Application icon
  69.         self.setWindowIcon(QIcon(load_pixmap('prog', '48x48')))
  70.  
  71.  
  72.     #
  73.     # SOURCE PAGE
  74.     #
  75.     def showSourcePage(self):
  76.         if self.install_mode == PLUGIN_REQUIRED:
  77.             self.TitleLabel.setText(self.__tr("An additional driver plug-in is required to operate this printer. You may download the plug-in directly from an HP authorized server (recommended), or, if you already have a copy of the file, you can specify a path to the file (advanced)."))
  78.             self.SkipRadioButton.setEnabled(False)
  79.  
  80.         elif self.install_mode == PLUGIN_OPTIONAL:
  81.             self.TitleLabel.setText(self.__tr("An optional driver plug-in is available to enhance the operation of this printer. You may download the plug-in directly from an HP authorized server (recommended), skip this installation (not recommended), or, if you already have a copy of the file, you can specify a path to the file (advanced)."))
  82.  
  83.         self.connect(self.DownloadRadioButton, SIGNAL("toggled(bool)"), self.DownloadRadioButton_toggled)
  84.         self.connect(self.CopyRadioButton, SIGNAL("toggled(bool)"), self.CopyRadioButton_toggled)
  85.         self.connect(self.SkipRadioButton, SIGNAL("toggled(bool)"), self.SkipRadioButton_toggled)
  86.         self.connect(self.PathLineEdit, SIGNAL("textChanged(const QString &)"), self.PathLineEdit_textChanged)
  87.         self.connect(self.BrowseToolButton, SIGNAL("clicked()"), self.BrowseToolButton_clicked)
  88.  
  89.         self.BrowseToolButton.setIcon(QIcon(load_pixmap('folder_open', '16x16')))
  90.  
  91.         self.displayPage(PAGE_SOURCE)
  92.  
  93.  
  94.     def DownloadRadioButton_toggled(self, b):
  95.         if b:
  96.             self.PathLineEdit.setEnabled(False)
  97.             self.BrowseToolButton.setEnabled(False)
  98.             self.NextButton.setEnabled(True)
  99.             try:
  100.                 self.PathLineEdit.setStyleSheet("")
  101.             except AttributeError:
  102.                 pass
  103.             self.plugin_path = None
  104.  
  105.  
  106.     def CopyRadioButton_toggled(self, b):
  107.         if b:
  108.             self.PathLineEdit.setEnabled(True)
  109.             self.BrowseToolButton.setEnabled(True)
  110.             self.plugin_path = unicode(self.PathLineEdit.text())
  111.             self.setPathIndicators()
  112.  
  113.  
  114.     def SkipRadioButton_toggled(self, b):
  115.         if b:
  116.             self.PathLineEdit.setEnabled(False)
  117.             self.BrowseToolButton.setEnabled(False)
  118.             self.NextButton.setEnabled(True)
  119.             try:
  120.                 self.PathLineEdit.setStyleSheet("")
  121.             except AttributeError:
  122.                 pass
  123.             self.plugin_path = None
  124.  
  125.  
  126.     def PathLineEdit_textChanged(self, t):
  127.         self.plugin_path = unicode(t)
  128.         self.setPathIndicators()
  129.  
  130.  
  131.     def setPathIndicators(self):
  132.         ok = True
  133.         if not self.plugin_path or (self.plugin_path and os.path.isdir(self.plugin_path)):
  134.             self.PathLineEdit.setToolTip(self.__tr("You must specify a path to the '%1' file.").arg(self.core.plugin_name))
  135.             ok = False
  136.         elif os.path.basename(self.plugin_path) != self.core.plugin_name:
  137.             self.PathLineEdit.setToolTip(self.__tr("The plugin filename must be '%1'.").arg(self.core.plugin_name))
  138.             ok = False
  139.  
  140.         if not ok:
  141.             try:
  142.                 self.PathLineEdit.setStyleSheet("background-color: yellow; ")
  143.             except AttributeError:
  144.                 pass
  145.             self.NextButton.setEnabled(False)
  146.         else:
  147.             try:
  148.                 self.PathLineEdit.setStyleSheet("")
  149.             except AttributeError:
  150.                 pass
  151.             self.NextButton.setEnabled(True)
  152.             self.PathLineEdit.setToolTip(QString(""))
  153.  
  154.  
  155.     def BrowseToolButton_clicked(self):
  156.         t = unicode(self.PathLineEdit.text())
  157.  
  158.         if not os.path.exists(t):
  159.             path = unicode(QFileDialog.getOpenFileName(self, self.__tr("Select Plug-in File"),
  160.                                                 user_conf.workingDirectory(),
  161.                                                 self.__tr("Plugin Files (*.run)")))
  162.  
  163.         if path:
  164.             self.plugin_path = path
  165.             self.PathLineEdit.setText(self.plugin_path)
  166.             user_conf.setWorkingDirectory(self.plugin_path)
  167.  
  168.         self.setPathIndicators()
  169.  
  170.     #
  171.     # Misc
  172.     #
  173.  
  174.     def displayPage(self, page):
  175.         self.updateStepText(page)
  176.         self.StackedWidget.setCurrentIndex(page)
  177.  
  178.  
  179.     def CancelButton_clicked(self):
  180.         self.close()
  181.  
  182.  
  183.     def NextButton_clicked(self):
  184.         if self.SkipRadioButton.isChecked():
  185.             log.debug("Skipping plug-in installation.")
  186.             self.close()
  187.             return
  188.  
  189.         if self.plugin_path is None: # download
  190.             # read plugin.conf (local or on sf.net) to get plugin_path (http://)
  191.             plugin_conf_url = self.core.get_plugin_conf_url()
  192.  
  193.             if plugin_conf_url.startswith('file://'):
  194.                 pass
  195.             else:
  196.                 log.info("Checking for network connection...")
  197.                 ok = self.core.check_network_connection()
  198.  
  199.                 if not ok:
  200.                     log.error("Network connection not detected.")
  201.                     FailureUI(self, self.__tr("Network connection not detected."))
  202.                     self.close()
  203.                     return
  204.  
  205.             log.info("Downloading configuration file from: %s" % plugin_conf_url)
  206.             self.plugin_path, size, checksum, timestamp, ok = self.core.get_plugin_info(plugin_conf_url,
  207.                 self.plugin_download_callback)
  208.  
  209.             print self.plugin_path, size, checksum, timestamp, ok
  210.  
  211.             log.debug("path=%s, size=%d, checksum=%s, timestamp=%f, ok=%s" %
  212.                       (self.plugin_path, size, checksum, timestamp, ok))
  213.  
  214.             if not self.plugin_path.startswith('http://') and not self.plugin_path.startswith('file://'):
  215.                 self.plugin_path = 'file://' + self.plugin_path
  216.  
  217.         else: # path
  218.             if not self.plugin_path.startswith('http://'):
  219.                 self.plugin_path = 'file://' + self.plugin_path
  220.  
  221.             size, checksum, timestamp = 0, '', 0
  222.  
  223.         if self.plugin_path.startswith('file://'):
  224.             pass
  225.         else:
  226.             log.info("Checking for network connection...")
  227.             ok = self.core.check_network_connection()
  228.  
  229.             if not ok:
  230.                 log.error("Network connection not detected.")
  231.                 FailureUI(self, self.__tr("Network connection not detected."))
  232.                 self.close()
  233.                 return
  234.  
  235.         log.info("Downloading plug-in from: %s" % self.plugin_path)
  236.  
  237.         ok, local_plugin = self.core.download_plugin(self.plugin_path, size, checksum, timestamp,
  238.             self.plugin_download_callback)
  239.  
  240.         if not ok:
  241.             log.error("Plug-in download failed: %s" % local_plugin)
  242.             FailureUI(self, self.__tr("Plug-in download failed."))
  243.             self.close()
  244.             return
  245.  
  246.         if not self.core.run_plugin(GUI_MODE, self.plugin_install_callback):
  247.             FailureUI(self, self.__tr("Plug-in install failed."))
  248.             self.close()
  249.             return
  250.  
  251.         cups_devices = device.getSupportedCUPSDevices(['hp'])
  252.         for dev in cups_devices:
  253.             mq = device.queryModelByURI(dev)
  254.  
  255.             if mq.get('fw-download', False):
  256.                 # Download firmware if needed
  257.                 log.info(log.bold("\nDownloading firmware to device %s..." % dev))
  258.                 try:
  259.                     d = None
  260.                     try:
  261.                         d = device.Device(dev)
  262.                     except Error:
  263.                         log.error("Error opening device.")
  264.                         continue
  265.  
  266.                     if d.downloadFirmware():
  267.                         log.info("Firmware download successful.\n")
  268.  
  269.                 finally:
  270.                     if d is not None:
  271.                         d.close()
  272.  
  273.         SuccessUI(self, self.__tr("Plug-in install successful."))
  274.         self.result = True
  275.         self.close()
  276.  
  277.  
  278.     def plugin_download_callback(self, c, s, t):
  279.         pass
  280.  
  281.  
  282.     def plugin_install_callback(self, s):
  283.         print s
  284.  
  285.  
  286.     def updateStepText(self, p):
  287.         self.StepText.setText(self.__tr("Step %1 of %2").arg(p+1).arg(PAGE_MAX+1))
  288.  
  289.  
  290.     def __tr(self,s,c = None):
  291.         return qApp.translate("PluginDialog",s,c)
  292.  
  293.  
  294.